package com.ansion.utils;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;


public final class CocoLocalBroadcastUtil {
    public static void broadcastNoSDCard() {
        //目前这个broadcast无用
//        Intent intent = new Intent(AppConstants.CocoSystemAction.ACTION_NOSDCARD);
//        LocalBroadcastManager.getInstance(ResourceUtils.getContext())
//                .sendBroadcast(intent);
    }

    /**
     * 发送请求返回广播
     *
     * @param intent
     * @param code
     */
    public static void sendLocalBroadcast(Intent intent, String extra, int code) {
        intent.putExtra(extra, code);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).sendBroadcast(intent);
    }

    /**
     * 发送请求返回广播
     *
     * @param intent
     */
    public static void sendLocalBroadcast(Intent intent) {
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).sendBroadcast(intent);
    }

    /**
     * 发送请求返回广播
     *
     * @param Action
     * @param code
     */
    public static void sendLocalBroadcast(String Action, int code) {
        Intent intent = new Intent(Action);
        intent.putExtra("ret", code);
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).sendBroadcast(intent);
    }

    /**
     * 注册请求返回的广播
     *
     * @param mBroadcastReceiver
     * @param filterAction
     */
    public static void registerLocalBroadCast(BroadcastReceiver mBroadcastReceiver, String... filterAction) {
        IntentFilter filter = new IntentFilter();
        for (String action : filterAction) {
            filter.addAction(action);
        }
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).registerReceiver(mBroadcastReceiver, filter);
    }

    /**
     * 注册请求返回的广播
     *
     * @param mBroadcastReceiver
     * @param filter
     */
    public static void registerLocalBroadCast(BroadcastReceiver mBroadcastReceiver,
                                              IntentFilter filter) {
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).registerReceiver(
                mBroadcastReceiver, filter);
    }

    /**
     * 解注册广播
     *
     * @param mBroadcastReceiver
     */
    public static void unregisterLocalBroadCast(BroadcastReceiver mBroadcastReceiver) {
        LocalBroadcastManager.getInstance(ResourceUtils.getContext()).unregisterReceiver(mBroadcastReceiver);
    }

}
